/*------------------------------------------------------------------------------*\
**   Copyright 2008 Jonhnny Weslley                                             **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package shibernate.contacts

import scala.reflect.BeanProperty
import javax.persistence.{Id, Entity, Embeddable, Embedded, ManyToOne}

@Entity
class Contact {
  @Id var id: Int = _
  @BeanProperty var name: String = _
  @Embedded @BeanProperty var address: Address = new Address
  @Embedded @BeanProperty var info: ContactInfo = new ContactInfo
  override def toString = id + ", name: " + name + ", city: " + address.city
}

object Contact {
  def apply(id: Int, name: String) = {
    new ContactBuilder(id, name)
  }
}

class ContactBuilder(id: Int, name: String) {
  
  val contact = new Contact
  contact.id = id
  contact.name = name
  
  def apply() = {
    contact
  }
  
  def address(street: String, zipCode: String, city: City) = {
    contact.address.street = street
    contact.address.zipCode = zipCode
    contact.address.city = city
    this
  }
  
  def homePhoneNumber(number: String) = {
    contact.info.homePhoneNumber = number
    this
  }

  def workPhoneNumber(number: String) = {
    contact.info.workPhoneNumber = number
    this
  }

  def faxNumber(number: String) = {
    contact.info.faxNumber = number
    this
  }

  def email(email: String) = {
    contact.info.emailAddress = email
    this
  }

}

@Embeddable
class ContactInfo {
  @BeanProperty var homePhoneNumber: String = _
  @BeanProperty var workPhoneNumber: String = _
  @BeanProperty var faxNumber: String = _
  @BeanProperty var emailAddress: String = _
}

@Embeddable
class Address {
  @BeanProperty var street: String = _ 
  @BeanProperty var zipCode: String = _
  @ManyToOne @BeanProperty var city: City = _
}

@Entity
class City {
  @Id var id: Int = _
  @BeanProperty var name: String = _
  @BeanProperty var state: String = _
  override def toString = id + ", " + name
}

object City {
 def apply(id: Int, name: String, state: String) = {
   val c = new City
   c.id = id
   c.name = name
   c.state = state
   c
 }
}
